
import UIKit
protocol popViewDelegete{
    func popViewDismiss(strs:String)
}
class popView: UIView, seleTabViewItem {
    var delegete:popViewDelegete?
    var heights:CGFloat!
    
    // 添加一个灰色图片控件
    var containerView:UIView = UIView()
    func setContainerView(){
        //设置背景图片
        containerView.userInteractionEnabled = true // 开启交互
        containerView.backgroundColor = UIColor(white: 0, alpha: 0.4)
        self.addSubview(containerView)
    }
    // 添加内容控制器
    var contentController:popTableViewController!
    // 添加内容视图
    var content:UIView = UIView()
    var btnView:UIButton?
    func setContent(frame: CGRect, frames: CGRect){
        contentController = popTableViewController()
//        contentController.dataArray = datas
        contentController.view.frame.size = CGSizeMake(frame.width, 200)
        content = contentController.view
        // 调整内容的位置
        content.frame.origin.x = 0;
        content.frame.origin.y = 0;
        // 设置灰色的高度
        self.containerView.frame.size.height = CGRectGetMaxY(content.frame) + 500
        // 设置灰色的宽度
        self.containerView.frame.size.width = CGRectGetMaxX(content.frame)
        content.backgroundColor = UIColor.whiteColor()
        contentController.seletab = self
        // 添加内容到灰色图片中
        self.containerView.addSubview(content)
    }
    
    func seleTabViewItemString(str:String) {
        self.dismiss(str)
    }

    func showFrom(form: UIButton, datas: NSArray){
 
        // 1.获得最上面的窗口
        var window:UIWindow = (UIApplication.sharedApplication().windows.last as? UIWindow)!
        // 2.添加自己到窗口上
        window.addSubview(self)
        // 3.设置尺寸
        self.frame = window.bounds
        // 4.调整灰色图片的位置
        // 默认情况下，frame是以父控件左上角为坐标原点
        // 转换坐标系
        var newFrame:CGRect = form.convertRect(form.bounds, toView: window)
//        self.containerView.center.x = CGRectGetMidX(newFrame)
        self.containerView.frame.origin.y = CGRectGetMaxY(newFrame)
        self.contentController.dataArray = datas
        self.btnView = form
    }
    
    func dismiss(sting:String){
        self.removeFromSuperview()
        self.delegete?.popViewDismiss(sting)
        self.btnView?.setTitle(sting, forState: UIControlState.Normal)
    }
    
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        self.removeFromSuperview()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setContainerView()
        setContent(frame, frames: frame)
        // 清除颜色
        self.backgroundColor = UIColor.clearColor()
        
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
